package com.link.play.fit.ai.ui.main.saveFood.details.modifyPage

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import com.link.play.fit.ai.R
import com.link.play.fit.ai.base.BaseActivity
import com.link.play.fit.ai.bean.CalendarStatusData
import com.link.play.fit.ai.databinding.ModifyCarbsLayoutBinding

class ModifyCarbsAc : BaseActivity<ModifyCarbsLayoutBinding>(ModifyCarbsLayoutBinding::inflate) {
    private var mValue = ""
    private var calendarStatusData: CalendarStatusData? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initListener()
        initData()
    }

    private fun initListener() {
        with(binding) {
            backImage.setOnClickListener {
                finish()
            }
            doneText.setOnClickListener {
                val intent = Intent().apply {
                    putExtra("result_key", binding.editCalValue.text.toString())
                }
                setResult(Activity.RESULT_OK, intent)
                finish()
            }
            revertText.setOnClickListener {
                binding.editCalValue.setText(mValue)
            }
        }
    }

    private fun initData() {
        mValue = intent.getStringExtra("value").toString()
        calendarStatusData = intent.getSerializableExtra("bean") as CalendarStatusData?
        binding.caloriesValueText.text = mValue

        binding.editCalValue.setText(mValue)
        calendarStatusData?.let {
            binding.carbProgress.max = it.carbsDaily
            binding.carbProgress.progress = it.consumeCarbsDaily
            if (it.carbsDaily > it.consumeCarbsDaily) {
                binding.outleftText.text =
                    getString(R.string.out_of_left, "${it.carbsDaily - it.consumeCarbsDaily}")
            } else {
                binding.outleftText.text =
                    getString(R.string.out_of_over, "${it.consumeCarbsDaily - it.carbsDaily}")
            }
        }
    }
}